package cn.skyquant.quant4j.api.ylzc;

import cn.skyquant.quant4j.api.dto.ResultDTO;
import cn.skyquant.quant4j.api.enums.BorkerType;
import cn.skyquant.quant4j.api.service.BaseServiceProxy;
import cn.skyquant.quant4j.sdk.enums.Status;
import cn.skyquant.quant4j.sdk.enums.YlzcType;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class YlzcServiceProxy extends BaseServiceProxy implements YlzcService {
    private static final String url = default_urlfix+"/ylzc";

    public YlzcServiceProxy(String accountCode, BorkerType borkerType) {
        super(accountCode, borkerType);
    }

    @Override
    public ResultDTO<YlzcMap> query(String symbol, Integer period, Date begin, Date end) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("symbol",symbol);
        paramMap.put("period",period.toString());
        paramMap.put("begin",String.valueOf(begin.getTime()));
        paramMap.put("end",String.valueOf(end.getTime()));
        JSONObject jsonObject = get(url,paramMap);
        YlzcMap ylzcMap = new YlzcMap();
        if(jsonObject!=null){
            boolean ok = jsonObject.getBoolean("ok");
            if(ok){
                JSONObject data = jsonObject.getJSONObject("data");
                JSONArray ylw = data.getJSONArray("ylw");
                JSONArray zcw = data.getJSONArray("zcw");
                for(int i=0;i<ylw.size();i++){
                    JSONObject ylwJson = ylw.getJSONObject(i);
                    YlzcDTO ylzcDTO = convertJsonToYlzc(ylwJson);
                    ylzcMap.addYlw(ylzcDTO);
                }
                for(int i=0;i<zcw.size();i++){
                    JSONObject zcwJson = zcw.getJSONObject(i);
                    YlzcDTO ylzcDTO = convertJsonToYlzc(zcwJson);
                    ylzcMap.addZcw(ylzcDTO);
                }
            }
        }
        return ResultDTO.success(ylzcMap);
    }

    private static YlzcDTO convertJsonToYlzc(JSONObject jsonObject){
        YlzcDTO ylzcDTO = new YlzcDTO();
        ylzcDTO.id = jsonObject.getInteger("id");
        ylzcDTO.level = jsonObject.getInteger("level");
        ylzcDTO.period = jsonObject.getInteger("period");
        ylzcDTO.symbol = jsonObject.getString("symbol");
        ylzcDTO.value = jsonObject.getDoubleValue("value");
        ylzcDTO.thedate = jsonObject.getDate("thedate");
        ylzcDTO.status = Status.valueOf(jsonObject.getString("status"));
        ylzcDTO.type = YlzcType.valueOf(jsonObject.getString("type"));
        return ylzcDTO;
    }

    @Override
    public ResultDTO<Void> insert(String symbol, Integer period, Date thedate, YlzcType type, int level, double value) {
        return ResultDTO.error;
    }

    @Override
    public ResultDTO<YlzcDTO> get(Integer id) {
        return ResultDTO.error;
    }

    @Override
    public ResultDTO<Void> active(Integer id) {
        return ResultDTO.error;
    }

    @Override
    public ResultDTO<Void> disable(Integer id) {
        return ResultDTO.error;
    }

    @Override
    public ResultDTO<Void> update(Integer id, int newLeve, double newValue) {
        return ResultDTO.error;
    }

}
